//
//  LCHomeImageTableViewCell.m
//  段子手
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCHomeImageTableViewCell.h"

@interface LCHomeImageTableViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UIImageView *imgVUser;
@property (nonatomic, retain) UILabel *labelUserName;
@property (nonatomic, retain) UILabel *labelText;
@property (nonatomic, retain) UIButton *buttonKind;
@property (nonatomic, retain) UIButton *buttonZan;
@property (nonatomic, retain) UILabel *labelZan;
@property (nonatomic, retain) UIButton *buttonLow;
@property (nonatomic, retain) UILabel *labelLow;
@property (nonatomic, retain) UIButton *buttonHot;
@property (nonatomic, retain) UILabel *labelHot;

@property (nonatomic, retain) UITableView *tableViewComment;

@property (nonatomic, assign) CGRect rectText;
@property (nonatomic, assign) CGRect rectButton;

@property (nonatomic, assign)CGRect rectCommentText;

@property (nonatomic, assign)CGFloat tableViewCommentH;

@property (nonatomic, retain) UIImageView *imgV;


@end


@implementation LCHomeImageTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;
}


- (void)setModel:(LCHomeTableViewPassageModel *)model{
    
    _model = model;
    
    [self setModelValue];
    
    //    [_tableViewComment reloadData];
    //    [self reloadInputViews];
    
    
}


#pragma mark - 用model给控件赋值
- (void)setModelValue{
    
    //    获取评论tableView的自适应高度
    self.tableViewCommentH = 0;
    
    if (_model.comments.count != 0) {
        
        for (int i = 0; i < _model.comments.count; i++) {
            NSDictionary *arr  = [NSDictionary dictionaryWithDictionary:_model.comments[i]];
            NSString *str = arr[@"text"];
            CGRect rect = [WxlAPPTools scripeSuitWidthWithString:str Font:13 Size:CGSizeMake(SCREENW - 20 - 60, 0)];
            _tableViewCommentH += rect.size.height + 50;
            
        }
    }
    
    
    
    //    获取段子内容的自适应高度
    self.rectText = [WxlAPPTools scripeSuitWidthWithString:_model.text Font:14 Size:CGSizeMake(SCREENW - 40, 0)];
    
    
    //    NSLog(@"%@", _model.category_name);
    self.rectButton = [WxlAPPTools scripeSuitWidthWithString:_model.category_name Font:10 Size:CGSizeMake(0, 20)];
    
    
    [_imgVUser sd_setImageWithURL:[NSURL URLWithString:_model.user[@"avatar_url"]]];
    
    
    _labelUserName.text = _model.user[@"name"];
    
    
    _labelText.text = _model.text;
    _labelText.frame = CGRectMake(20, _imgVUser.frame.size.height + _imgVUser.frame.origin.y + 15, SCREENW - 40, _rectText.size.height);
    
    
    [_buttonKind setTitle:_model.category_name forState:UIControlStateNormal];
    _buttonKind.frame = CGRectMake(20, _labelText.frame.origin.y + _labelText.frame.size.height + 15, _rectButton.size.width + 20, 20);
    
   
    CGFloat imgVH = (SCREENW - 40) * [_model.large_image[@"height"] floatValue] / [_model.large_image[@"width"] floatValue];
    
    _imgV.frame = CGRectMake(20, _buttonKind.frame.size.height + _buttonKind.frame.origin.y + 10, SCREENW - 40, imgVH);
    
    
    if ([[NSString stringWithFormat:@"%@",  _model.media_type]  isEqualToString:@"2"]){
        
        dispatch_async(dispatch_get_main_queue(), ^{
             [_imgV sd_setImageWithURL:[NSURL URLWithString:_model.large_image[@"url_list"][0][@"url"] ]placeholderImage:PLACEHOLDERIMGV];
        });
        
   
//        NSLog(@"%@", _model.large_image[@"url_list"][0][@"url"]);
    }else if ([[NSString stringWithFormat:@"%@",  _model.media_type]  isEqualToString:@"1"]){
        
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_model.large_image[@"url_list"][0][@"url"]]];

        dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *image = [UIImage imageWithWebPData:data];
        _imgV.image = image;
        
        });
        
     
    }

    
    _buttonZan.frame = CGRectMake(20, _imgV.frame.size.height + _imgV.frame.origin.y, 30, 30);
    
    _labelZan.text = [NSString stringWithFormat:@"%@", _model.digg_count];
    _labelZan.frame = CGRectMake(_buttonZan.frame.origin.x + _buttonZan.frame.size.width, _buttonZan.frame.origin.y + 10, 45, 15);
    
    _buttonLow.frame = CGRectMake(_labelZan.frame.origin.x + _labelZan.frame.size.width + 4, _labelZan.frame.origin.y - 5, 30, 30);
    _labelLow.text = [NSString stringWithFormat:@"%@", _model.bury_count];
    _labelLow.frame = CGRectMake(_buttonLow.frame.origin.x
                                 + _buttonLow.frame.size.width, _buttonLow.frame.origin.y  + 5, 45, 15);
    
    
    _buttonHot.frame = CGRectMake(_labelLow.frame.origin.x + _labelLow.frame.size.width + 4, _labelLow.frame.origin.y - 6, 30, 30);
    _labelHot.text = [NSString stringWithFormat:@"%@", _model.comment_count];
    _labelHot.frame = CGRectMake(_buttonHot.frame.origin.x + _buttonHot.frame.size.width, _buttonHot.frame.origin.y + 6, 45, 15);
    
    //解决评论tableView的重用问题
    //    if (_tableViewComment) {
    //        _tableViewComment.frame = CGRectMake(10, _buttonZan.frame.origin.y + _buttonZan.frame.size.height, SCREENW - 20, _tableViewCommentH);
    //
    //    }
    
    //    如果评论数组不为空， 则创建评论tableView
    
    if (self.tableViewComment) {
        [_tableViewComment removeFromSuperview];
    }
    
    
    if (_model.comments.count != 0 ) {
        
        [self createTableViewComment];
        
    }
    
}



#pragma mark - 创建评论的tableView
- (void)createTableViewComment{
    
    if (self.tableViewComment) {
        [_tableViewComment removeFromSuperview];
    }
    
    self.tableViewComment = [[UITableView alloc]initWithFrame:CGRectMake(10, _buttonZan.frame.origin.y + _buttonZan.frame.size.height, SCREENW - 20, _tableViewCommentH) style:UITableViewStylePlain];
    
    _tableViewComment.bounces = NO;
    
    _tableViewComment.dataSource = self;
    _tableViewComment.delegate = self;
    
    [_tableViewComment registerClass:[LCContentTableViewCell class] forCellReuseIdentifier:@"commentCell"];
    //    _tableViewComment.backgroundColor = [UIColor colorWithRed:0.4 green:0.9 blue:0.4 alpha:1];
    
    //    self.tableViewCommentH = 0.0;
    
    [self.contentView addSubview:_tableViewComment];
    
    
}


#pragma mark - 评论tableView的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCContentTableViewCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    
    LCHomeCommentModel *modelC = [LCHomeCommentModel modelWithDic:_model.comments[indexPath.row]];
    
    //    NSLog(@"%@", modelC);
    
    commentCell.model = modelC;
    
    //    commentCell.backgroundColor = [UIColor colorWithRed:0.8 green:0.4 blue:0.4 alpha:0.1];
    
    
    return commentCell;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_model.comments.count > 2) {
        return 2;
    }else return _model.comments.count;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *textStr = _model.comments[indexPath.row][@"text"];
    
    self.rectCommentText = [WxlAPPTools scripeSuitWidthWithString:textStr Font:13 Size:CGSizeMake(SCREENW - 80, 0)];
    
    NSLog(@"%f", 50 + _rectCommentText.size.height);
    
    return 50 + _rectCommentText.size.height;
}




- (void)create{
    
    //    用户头像
    self.imgVUser = [[UIImageView alloc]init];
    [self.contentView addSubview:_imgVUser];
    _imgVUser.frame =CGRectMake(20, 20, 30, 30);
    _imgVUser.layer.cornerRadius = 15;
    _imgVUser.layer.masksToBounds = YES;
    
    //    用户名
    self.labelUserName = [[UILabel alloc]init];
    [self.contentView addSubview:_labelUserName];
    _labelUserName.frame = CGRectMake(_imgVUser.frame.size.width + 20 + 20, 20, SCREENW -  _imgVUser.frame.size.width - 20 - 20 - 20, 30);
    
    
    //    段子内容
    self.labelText = [[UILabel alloc]init];
    [self.contentView addSubview:_labelText];
    _labelText.font = [UIFont systemFontOfSize:14];
    _labelText.numberOfLines = 100;
    
    
    //    种类按钮
    self.buttonKind = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonKind];
    _buttonKind.layer.cornerRadius = 10;
    _buttonKind.layer.borderColor = [UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1].CGColor;
    _buttonKind.layer.borderWidth = 0.5;
    [_buttonKind setTitleColor:[UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1] forState:UIControlStateNormal];
    _buttonKind.titleLabel.font = [UIFont systemFontOfSize:10];
    
    
    self.imgV = [[UIImageView alloc]init];
    [self.contentView addSubview:_imgV];
    
    
    //    点赞按钮
    self.buttonZan = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonZan];
    [_buttonZan setImage:[UIImage imageNamed:@"RC32NLI__LBBJR]ET5I%D{5.png"] forState:UIControlStateNormal];
    
    
    
    self.labelZan = [[UILabel alloc]init];
    [self.contentView addSubview:_labelZan];
    _labelZan.font = [UIFont systemFontOfSize:12];
    _labelZan.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    
    
    
    self.buttonLow = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: _buttonLow];
    [_buttonLow setImage:[UIImage imageNamed:@"51FYKZ@K77SS}A0@{G_C]C7.png"] forState:UIControlStateNormal];
    
    
    self.labelLow = [[UILabel alloc]init];
    [self.contentView addSubview:_labelLow];
    _labelLow.font = [UIFont systemFontOfSize:12];
    _labelLow.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    
    
    self.buttonHot = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonHot];
    [_buttonHot setImage:[UIImage imageNamed:@"LK@AF$6X){$X6E2WH3}5MUI.png"] forState:UIControlStateNormal];
    
    
    self.labelHot = [[UILabel alloc]init];
    [self.contentView addSubview:_labelHot];
    _labelHot.font = [UIFont systemFontOfSize:12];
    _labelHot.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    
//    //    如果评论数组不为空， 则创建评论tableView
//    if (_model.comments.count != 0 ) {
//        
//        [self createTableViewComment];
//        
//    }
    
    
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
